package com.gbq.service.impl;

import com.gbq.redisVo.SynEntity;
import com.gbq.service.RedisService;
import com.gbq.util.BaseUtils;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class RedisServiceImpl implements RedisService{

    @Value("${tablelists}")
    private  String tablelists;

    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    RedisService redisService;

    @Override
    public String put(SynEntity synEntity) {

        if (BaseUtils.isTable(tablelists,synEntity.getField())==-1){
            return "unsupport syn";
        }else {
            try {
                RedisSerializer<String> serializer = new StringRedisSerializer();
                ValueOperations<String,String> valueOperations = redisTemplate.opsForValue();
                redisTemplate.setKeySerializer(serializer);
                redisTemplate.setValueSerializer(serializer);
                //将对象转成json字符串放入redis
                Map<String,Object> map = synEntity.getObj();
                JSONObject json = JSONObject.fromObject(map);
                String strJson=json.toString();
                valueOperations.set(synEntity.getField()+map.get("card_no"),strJson);
                return "redis inserted";
            } catch (Exception e) {
                log.info(e.toString());
                return "redis failed";
            }
        }

    }

    @Override
    public String delete(SynEntity synEntity) {

        if (BaseUtils.isTable(tablelists,synEntity.getField())==-1){
            return "unsupport syn";
        }else {
            try {
                RedisSerializer<String> serializer = new StringRedisSerializer();
                Map<String,Object> map = synEntity.getObj();
                redisTemplate.setKeySerializer(serializer);
                redisTemplate.setValueSerializer(serializer);
                redisTemplate.delete(synEntity.getField()+map.get("card_no"));
                return "redis deleted";
            } catch (Exception e) {
                log.info(e.toString());
                return "redis failed";
            }

        }

    }
    @Override
    public String update(SynEntity synEntity) {

        if (BaseUtils.isTable(tablelists,synEntity.getField())==-1){
            return "unsupport syn";
        }else {
            try {
                redisService.put(synEntity);
                return "redis updated";
            } catch (Exception e) {
                log.info(e.toString());
                return "redis failed";
            }
        }

    }





    @Override
    public Object get(String field) {
        RedisSerializer<String> serializer = new StringRedisSerializer();
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.setHashKeySerializer(serializer);
        redisTemplate.setHashValueSerializer(serializer);

//        Long jsonstr = hashOperations.size(field);     //获取当前field的所有条目数量

        List lists = hashOperations.values(field);       //获取当前field中所有数据详细
        //转换成json对象数组
        Object objs =  lists.stream().map(list->new JSONObject().fromObject(list)).collect(Collectors.toList());
        return objs ;
    }
    @Override
    public Object getOne(String field, String key) {
        RedisSerializer<String> serializer = new StringRedisSerializer();
        HashOperations<String,String,String> hashOperations = redisTemplate.opsForHash();
        redisTemplate.setKeySerializer(serializer);
        redisTemplate.setHashKeySerializer(serializer);
        redisTemplate.setHashValueSerializer(serializer);


        String str = hashOperations.get(field,key);
        JSONObject jsonObject = new JSONObject();
        if (str==null){
            return null;
        }else {
            jsonObject = new JSONObject().fromObject(str);
            return jsonObject;
        }

    }

}
